<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Bean 作用域完全指南</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            position: relative;
            overflow: hidden;
        }
        
        .hero-gradient::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            bottom: -50%;
            left: -50%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 1px, transparent 1px);
            background-size: 50px 50px;
            animation: drift 20s linear infinite;
        }
        
        @keyframes drift {
            from { transform: translate(0, 0); }
            to { transform: translate(50px, 50px); }
        }
        
        .card-hover {
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }
        
        .card-hover:hover {
            transform: translateY(-8px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
        }
        
        .code-block {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            display: flex;
            align-items: center;
            padding: 0 16px;
        }
        
        .code-content {
            padding: 60px 24px 24px;
            color: #d4d4d4;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
            overflow-x: auto;
        }
        
        .scope-badge {
            display: inline-flex;
            align-items: center;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        
        .scope-singleton { background: #e3f2fd; color: #1565c0; }
        .scope-prototype { background: #f3e5f5; color: #6a1b9a; }
        .scope-request { background: #e8f5e9; color: #2e7d32; }
        .scope-session { background: #fff3e0; color: #e65100; }
        .scope-global { background: #fce4ec; color: #c2185b; }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .icon-singleton { background: #e3f2fd; color: #1565c0; }
        .icon-prototype { background: #f3e5f5; color: #6a1b9a; }
        .icon-request { background: #e8f5e9; color: #2e7d32; }
        .icon-session { background: #fff3e0; color: #e65100; }
        .icon-global { background: #fce4ec; color: #c2185b; }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 0.8;
            margin: 0.1em 0.1em 0 0;
            font-weight: 700;
            color: #667eea;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 relative">
        <div class="container mx-auto px-6 relative z-10">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Spring Bean 作用域
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">完全指南</span>
                </h1>
                <p class="text-xl md:text-2xl font-light opacity-90 leading-relaxed">
                    深入理解 Spring 框架中 Bean 的生命周期与可见性范围
                </p>
                <div class="mt-8 flex justify-center space-x-4">
                    <span class="scope-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-code mr-2"></i>Spring Framework
                    </span>
                    <span class="scope-badge bg-white bg-opacity-20 text-white">
                        <i class="fas fa-cube mr-2"></i>Bean Lifecycle
                    </span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto px-6 py-16 max-w-7xl">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8 md:p-12">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">在</span>Spring框架中，Bean的作用域定义了Bean实例的生命周期和可见性范围。理解不同的作用域对于构建高效、可扩展的应用程序至关重要。Spring提供了多种作用域选项，每种都有其特定的使用场景和优势。
                </p>
            </div>
        </section>

        <!-- Visual Diagram -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-xl p-8">
                <h2 class="text-3xl font-bold text-gray-800 mb-8 text-center">
                    <i class="fas fa-sitemap mr-3 text-purple-600"></i>Bean 作用域关系图
                </h2>
                <div class="mermaid">
                    graph TD
                        A[Spring Bean 作用域] --> B[Singleton<br/>单例模式]
                        A --> C[Prototype<br/>原型模式]
                        A --> D[Web 作用域]
                        D --> E[Request<br/>请求作用域]
                        D --> F[Session<br/>会话作用域]
                        D --> G[GlobalSession<br/>全局会话作用域]
                        
                        B --> B1[整个应用程序<br/>共享一个实例]
                        C --> C1[每次请求<br/>创建新实例]
                        E --> E1[每个HTTP请求<br/>创建新实例]
                        F --> F1[每个HTTP会话<br/>创建新实例]
                        G --> G1[全局HTTP会话<br/>创建新实例]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                        style B fill:#e3f2fd,stroke:#1565c0,stroke-width:2px
                        style C fill:#f3e5f5,stroke:#6a1b9a,stroke-width:2px
                        style D fill:#fafafa,stroke:#666,stroke-width:2px
                        style E fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px
                        style F fill:#fff3e0,stroke:#e65100,stroke-width:2px
                        style G fill:#fce4ec,stroke:#c2185b,stroke-width:2px
                </div>
            </div>
        </section>

        <!-- Scope Details -->
        <section class="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-16">
            <!-- Singleton Scope -->
            <div class="card-hover bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="p-8">
                    <div class="feature-icon icon-singleton">
                        <i class="fas fa-atom"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800 mb-2">
                        Singleton 作用域
                        <span class="scope-badge scope-singleton ml-3">默认</span>
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        在整个应用程序中只存在一个Bean实例。每次请求该Bean时，都会返回同一个实例。这是Spring的默认作用域，适用于无状态的服务类。
                    </p>
                    <div class="code-block">
                        <div class="code-content">
<pre>@Component
@Scope("singleton")  // 可省略，默认即为singleton
public class ExampleBean {
    // Bean properties and methods
}</pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Prototype Scope -->
            <div class="card-hover bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="p-8">
                    <div class="feature-icon icon-prototype">
                        <i class="fas fa-clone"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800 mb-2">
                        Prototype 作用域
                        <span class="scope-badge scope-prototype ml-3">原型</span>
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        每次请求Bean时，都会创建一个新的实例。IOC容器不会管理Bean的完整生命周期，每次使用都创建新的Bean实例，适用于有状态的对象。
                    </p>
                    <div class="code-block">
                        <div class="code-content">
<pre>@Component
@Scope("prototype")
public class ExampleBean {
    // Bean properties and methods
}</pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Request Scope -->
            <div class="card-hover bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="p-8">
                    <div class="feature-icon icon-request">
                        <i class="fas fa-globe"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800 mb-2">
                        Request 作用域
                        <span class="scope-badge scope-request ml-3">Web</span>
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        在每个HTTP请求中，都会创建一个新的Bean实例。该作用域仅适用于Spring MVC中的Web应用程序，适合处理请求相关的数据。
                    </p>
                    <div class="code-block">
                        <div class="code-content">
<pre>@Component
@Scope("request")
public class ExampleBean {
    // Bean properties and methods
}</pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Session Scope -->
            <div class="card-hover bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="p-8">
                    <div class="feature-icon icon-session">
                        <i class="fas fa-user-circle"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800 mb-2">
                        Session 作用域
                        <span class="scope-badge scope-session ml-3">Web</span>
                    </h3>
                    <p class="text-gray-600 mb-6 leading-relaxed">
                        在每个HTTP会话中，都会创建一个新的Bean实例。适用于需要在用户会话期间保持状态的场景，如购物车、用户偏好设置等。
                    </p>
                    <div class="code-block">
                        <div class="code-content">
<pre>@Component
@Scope("session")
public class ExampleBean {
    // Bean properties and methods
}</pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- GlobalSession Scope -->
        <section class="mb-16">
            <div class="card-hover bg-white rounded-2xl shadow-lg overflow-hidden">
                <div class="p-8">
                    <div class="flex items-start">
                        <div class="feature-icon icon-global mr-6">
                            <i class="fas fa-network-wired"></i>
                        </div>
                        <div class="flex-1">
                            <h3 class="text-2xl font-bold text-gray-800 mb-2">
                                GlobalSession 作用域
                                <span class="scope-badge scope-global ml-3">Portlet</span>
                            </h3>
                            <p class="text-gray-600 mb-6 leading-relaxed">
                                在全局HTTP会话中创建Bean实例。该作用域主要用于Portlet应用程序，在分布式Web应用中共享会话数据。需要在配置文件中启用全局会话支持。
                            </p>
                            <div class="code-block">
                                <div class="code-content">
<pre>@Component
@Scope("globalSession")
public class ExampleBean {
    // Bean properties and methods
}</pre>
                                